package com.tensquare.user.service;

import com.tensquare.user.dao.UserDao;
import com.tensquare.user.pojo.User;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/28 18:08
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder encoder;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * fetch data by rule id
     *
     * @description  删除  必须有admin角色才可以删除
     * @param
     * @return
     */
    public void deleteById(String id){

        //得到token
        String token = request.getParameter("admin_claims");
        if (token==null || "".equals(token)){
            throw new RuntimeException("权限不足！");
        }
        userDao.deleteById(id);
    }

    /**
     * fetch data by rule id
     *
     * @description  发送短信验证码
     * @param
     * @return
     */
    public void sendSms(String mobile){
        //1.生成6位短信验证码
        Random random = new Random();
        int max=999999;//最大数
        int min=100000;//最小数
        int code = random.nextInt(max);//随机生成
        if (code<min){
            code=code+min;//拼接
        }
        System.out.println(mobile+"收到的验证码是："+code);
        //2.将验证码放入redis
        redisTemplate.opsForValue().set("smscode_"+mobile,code+"",5, TimeUnit.MINUTES);//五分钟过期

        //3.将验证码和手机号发送到rabbitMQ当中
        Map<String,String> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("code",code+"");
        rabbitTemplate.convertAndSend("sms",map);

    }

    /**
     * fetch data by rule id
     *
     * @description  注册
     * @param
     * @return
     */
    public void add(User user, String code){
        //判断验证码是否正确
        String syscode =(String) redisTemplate.opsForValue().get("smscode_" + user.getMobile());
        //提取系统正确的验证码
        if(syscode==null){
            throw new RuntimeException("请点击获取短信验证码");
        }
        if(!syscode.equals(code)){
            throw new RuntimeException("验证码输入不正确");
        }
        user.setId( idWorker.nextId()+"" );
        user.setFollowcount(0+"");//关注数
        user.setFanscount(0+"");//粉丝数
        user.setOnline(0L+"");//在线时长
        user.setRegdate(new Date().toString());//注册日期
        user.setUpdatedate(new Date().toString());//更新日期
        user.setLastdate(new Date().toString());//最后登陆日期

        String newPassword = encoder.encode(user.getPassword());//加密
        user.setPassword(newPassword);
        userDao.save(user);
    }

    /**
     * 根据手机号和密码查询用户
     * @param mobile
     * @param password
     * @return
     */
    public User findByMobileAndPassword(String mobile,String password){
        User user = userDao.findByMobile(mobile);
        if(user!=null && encoder.matches(password,user.getPassword())){
            return user; }else{
            return null;
        }
    }
}